home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / ohlcpio.zip / DSTRING.C < prev    next >
C/C++ Source or Header  |  1990-07-02  |  2KB  |  92 lines

  1. /* dstring.c - The dynamic string handling routines used by cpio.
  2.    Copyright (C) 1988, 1989, 1990 Free Software Foundation, Inc.
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 1, or (at your option)
  7.    any later version.
  8.  
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. #include <stdio.h>
  19. #ifdef USG
  20. #include <string.h>
  21. #else
  22. #include <strings.h>
  23. #endif
  24. #include "dstring.h"
  25.  
  26. char *xmalloc ();
  27. char *xrealloc ();
  28.  
  29. /* Initialiaze dynamic string STRING with space for SIZE characters.  */
  30.  
  31. void
  32. ds_init (string, size)
  33.      dynamic_string *string;
  34.      int size;
  35. {
  36.   string->ds_length = size;
  37.   string->ds_string = (char *) xmalloc (size);
  38. }
  39.  
  40. /* Expand dynamic string STRING, if necessary, to hold SIZE characters.  */
  41.  
  42. void
  43. ds_resize (string, size)
  44.      dynamic_string *string;
  45.      int size;
  46. {
  47.   if (size > string->ds_length)
  48.     {
  49.       string->ds_length = size;
  50.       string->ds_string = (char *) xrealloc ((char *) string->ds_string, size);
  51.     }
  52. }
  53.  
  54. /* Dynamic string S gets a string from file F.  S will increase
  55.    in size during the function if the string from F is longer than
  56.    the current size of S.
  57.    Return NULL if end of file is detected.  Otherwise,
  58.    Return a pointer to the null-terminated string in S.  */
  59.  
  60. char *
  61. ds_fgets (f, s)
  62.      FILE *f;
  63.      dynamic_string *s;
  64. {
  65.   int insize;            /* Amount needed for line. */
  66.   int strsize;            /* Amount allocated for S. */
  67.   int next_ch;
  68.  
  69.   /* Initialize.  */
  70.   insize = 0;
  71.   strsize = s->ds_length;
  72.  
  73.   /* Read the input string.  */
  74.   next_ch = getc (f);
  75.   while (next_ch != '\n' && next_ch != EOF)
  76.     {
  77.       if (insize >= strsize - 1)
  78.     {
  79.       ds_resize (s, strsize * 2 + 2);
  80.       strsize = s->ds_length;
  81.     }
  82.       s->ds_string[insize++] = next_ch;
  83.       next_ch = getc (f);
  84.     }
  85.   s->ds_string[insize++] = '\0';
  86.  
  87.   if (insize == 1 && next_ch == EOF)
  88.     return NULL;
  89.   else
  90.     return s->ds_string;
  91. }
  92.